<?php
/**
 * 添加标签
 * @param string $title 标签名
 * @param string $stype 分类
 * @param int $postid 关联的文章id
 * @param int $postcid 文章栏目cid
 * @param string $comments 备注
 * @return bool|int
 */
function tag_add($title, $stype, $postid, $postcid, $comments = '')
{
    if (is_array($title)) {
        foreach ($title as $item) {
            tag_add($item, $stype, $postid, $postcid, $comments);
        }
        return true;
    }
    if (!trim($title))return false;
    $table_tags = tableex('tags');
    $count = M()->fetchcount("select count(id) from {$table_tags} where title=? and stype=? and postid=? and postcid=?", array($title, $stype, $postid, $postcid));
    if ($count == 0) {
        return qqq_add_art(17, array('stype' => $stype, 'postcid' => $postcid, 'postid' => $postid, 'title' => $title, 'comments' => $comments));
    } else {
        //已存在
        return false;
    }
}

/**
 * 移除标签
 * @param bool|string $title
 * @param bool|string $stype
 * @param bool|int $postcid
 * @param bool|int $postid
 * @return bool
 */
function tag_remove($title = false, $stype = false, $postcid = false, $postid = false)
{
    $table_tags = tableex('tags');

    $params = array();
    $where = array();
    if ($title !== false) {
        $params[] = $title;
        $where[] = "and title=?";
    }

    if ($stype !== false) {
        $params[] = $stype;
        $where[] = "and stype=?";
    }

    if ($postcid !== false) {
        $where[] = "and postcid=?";
        $params[] = $postcid;
    }

    if ($postid !== false) {
        $where[] = "and postid=?";
        $params[] = $postid;
    }

    if (empty($where)) {
        return false;
    }


    $whereStr = join(' ', $where);
//    var_dump($whereStr);
    M()->del($table_tags, '1=1 ' . $whereStr, $params);
    return true;
}

/**
 * 根据文章id查询标签
 * @param int $postid 文章id
 * @param bool $stype 分类
 * @param bool|int $postcid 文章栏目cid，false为不作为条件
 * @return array
 */
function tag_find_postid($postid, $stype = false, $postcid = false)
{
    $setting = array('postid' => $postid, 'ison' => 1);
    if ($postcid !== false) {
        $setting['postcid'] = $postcid;
    }

    if ($stype !== false) {
        $setting['stype'] = $stype;
    }
    $setting['order'] = 'id asc';

    return qqq_select_artlist(17, $setting);
}

/**
 * 根据文章栏目cid查询标签
 * @param bool|int $postcid 栏目cid
 * @param bool $distinct
 * @return array
 */
function tag_find_postcid($postcid = false, $distinct = true)
{
    $table_tags = tableex('tags');
    $where = "";
    $params = array();
    if ($postcid !== false) {
        $where = 'and postcid=?';
        $params[] = $postcid;
    }

    if ($distinct) {
        return M()->select('distinct title', $table_tags, 'where ison=1 ' . $where, $params);
    } else {
        return M()->select('*', $table_tags, 'where ison=1 ' . $where, $params);
    }
}

/**
 * 根据文章栏目cid查询标签
 * @param bool $postcid 文章cid
 * @param bool $show_stype
 * @return array
 */
function tag_find_count($postcid = false, $show_stype = false)
{
    if ($show_stype) {
        $show_stype = ',stype';
    }
    $table_tags = tableex('tags');
    if ($postcid !== false) {
        return M()->select('title' . $show_stype . ',count(title) as c ', $table_tags, 'where ison=1 and postcid=? GROUP BY title ORDER BY c desc', array($postcid));
    } else {
        return M()->select('title' . $show_stype . ',count(title) as c ', $table_tags, 'where ison=1 GROUP BY title ORDER BY c desc');
    }
}

/**
 * 根据标签title查询标签
 * @param string $title 标签
 * @param bool $postcid 可筛选分类
 * @return array
 */
function tag_find_title($title, $postcid = false)
{
    $setting = array('title' => $title, 'ison' => 1);
    if ($postcid !== false) {
        $setting['postcid'] = $postcid;
    }
    return qqq_select_artlist(17, $setting);
}

/**
 * 根据标签分类查询标签
 * @param string $stype 分类
 * @param bool $postcid 可筛选分类
 * @return array
 */
function tag_find_stype($stype, $postcid = false)
{
    $setting = array('stype' => $stype, 'ison' => 1);
    if ($postcid !== false) {
        $setting['postcid'] = $postcid;
    }
    return qqq_select_artlist(17, $setting);
}